<template>
  <div class="home">
    <button @click="handleClick('back')">返回上一页</button>
    <button @click="handleClick('push')">跳转到parent</button>
    <button @click="getInfo">请求数据</button>
    <h1> {{ food }} </h1>
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
import { getUserInfo } from '@/api/user'

export default {
  name: 'home',
  components: {
    HelloWorld
  },
  props: {
    food: {
      type: String,
      default: 'apple'
    }
  },
  beforeRouteEnter (to,from,next) {
    console.log(to.name)
    console.log("页面来自"+from.name)

    //vm 就是组件的实例
    next(vm => {
      console.log(vm)
    })
  },

  //当用户要离开当前页面时,编辑的内容还没有保存,询问用户是否离开
  beforeRouteLeave(to,from,next) {
    const leave = confirm('您确定要离开吗?')
    if (leave) next()
    else (false)
  },

  
  methods: {
    handleClick (type) {
      if (type === 'back')this.$router.back(-1)
      // else if (type === 'push')this.$router.push('/parent')
      else if (type === 'push')this.$router.push({
        name: 'parent'
      })
    },
    getInfo () {
      getUserInfo( { userId: 21 } ).then(res => {
        console.log('res: '+res)
      })
    }
  }
}
</script>
